﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LabelManager2;

namespace JYH.Framework
{
    public class CodeSoft
    {
        Application m_labApp;
        Document m_doc;
        public CodeSoft(string labFileName)
        {
            m_labApp = new Application();
            m_labApp.Documents.Open(labFileName, false);
            m_doc = m_labApp.ActiveDocument;
        }
        /// <summary>
        /// 选择打印机
        /// </summary>
        /// <param name="printerName"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public bool SwichToPrinter(string printerName)
        {
            try
            {
                return m_doc.Printer.SwitchTo(printerName);
            }
            catch (Exception ex)
            {

                throw new Exception("切换打印机失败！" + ex.Message + ex.StackTrace);
            }
        }
        /// <summary>
        /// 给数据源赋值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public string SetVariables(object key, string value, VariableType type = VariableType.FormVarible)
        {
            try
            {
                if (type == VariableType.FormVarible)
                {
                    m_doc.Variables.FreeVariables.Item(key).Value = value;
                }
                else if (type == VariableType.FormVarible)
                {
                    m_doc.Variables.FormVariables.Item(key).Value = value;
                }
                else if (type == VariableType.DataBaseVarible)
                {
                    m_doc.Variables.DatabaseVariables.Item(key).Value = value;
                }
                return "OK";
            }
            catch (Exception ex)
            {

                return "NG：数据源名称" + key + "赋值失败" + ex.Message;
            }
        }
        public string print(int quantity = 1)
        {
            try
            {
                m_doc.PrintDocument(quantity);
                return "OK";
            }
            catch (Exception ex)
            {

                return "NG：打印失败！" + ex.Message;
            }
        }

        /// <summary>
        /// 退出打印
        /// </summary>
        public void Quit()
        {
            if (m_labApp != null)
            {
                m_labApp.Documents.CloseAll(false);
                m_labApp.Quit();
                m_labApp = null;
                m_doc = null;
                GC.Collect(0);
            }
        }

        /// <summary>
        /// 获取数据源的类型
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public VariableType GetVariableType(string key)
        {
            VariableType variableType = VariableType.None;
            try
            {
                enumDataSource enumData = m_doc.Variables.Item(key).DataSource;
                switch (enumData)
                {
                    case enumDataSource.lppxDataSourceDate:
                        variableType = VariableType.DateVarible;
                        break;
                    case enumDataSource.lppxDataSourceFree:
                        variableType = VariableType.FreeVarible;
                        break;
                    case enumDataSource.lppxDataSourceForm:
                        variableType = VariableType.FormVarible;
                        break;
                    case enumDataSource.lppxDataSourceDataBase:
                        variableType = VariableType.DataBaseVarible;
                        break;
                    default:
                        variableType = VariableType.None;
                        break;
                }
                return variableType;
            }
            catch (Exception ex)
            {
                throw new Exception("无效的数据源名称:" + ex.Message + ex.StackTrace);
            }
        }

        /// <summary>
        /// 枚举数据源类型
        /// </summary>
        public enum VariableType
        {
            None = -1,
            FreeVarible = 0,
            FormVarible = 1,
            DataBaseVarible = 2,
            DateVarible = 3
        }



    }
}
